
$(function() {
  var $wrapper = $('#wrapper');

  $wrapper.find('#side-menu').metisMenu();

  addAction($wrapper);
  
});

function addAction(dom) {
  var $globalLoading = dom.find('#global-loading');

  dom.find('#side-menu').on('click', '[data-url]', function() {
    var $that = $(this);
    dom.find('#side-menu').find('.active').removeClass('active');
    $that.parent().parent().parent().parent().addClass('in');
    $that.addClass('active');
  });

  dom.on('click', '[data-url]', function() {

    var url = $(this).attr('data-url');

    $.post(url, function(data) {
      if(data) {
        dom.find('#page-wrapper').html(data);
      } else {
        alert('get data error !');
      }
    })
    return false;

  });

  dom.on('blur', '#many input', function() {
    var $that  = $(this);
    var fieldName = $that.attr('name');
    if(fieldName != 'crawlerCount') {
      return false;
    }

    var $hasFeedback = $that.parents('.has-feedback');
    var fieldVal = $.trim($that.val());

    if(!fieldVal || !Math.floor(dom.find('#crawler-count').val())){
      $hasFeedback.addClass('has-error').find('.glyphicon').addClass('glyphicon-remove');
      dom.find('#crawler-count-btn').addClass('disable');
    } else {
      $hasFeedback.removeClass('has-error').find('.glyphicon').removeClass('glyphicon-remove');
      dom.find('#crawler-count-btn').removeClass('disable');
    }

  });

  dom.on('click', '#crawler-count-btn', function() {    
    var crawlerCount = Math.floor(dom.find('#crawler-count').val());
    if(!crawlerCount || $(this).hasClass('disable')) {
      return false;
    }

    $globalLoading.fadeIn(90);

    $.post('/admin/crawlerManyNews', {crawlerCount: crawlerCount}, function(data) {
      $globalLoading.fadeOut(0);

      if(data) {
        //dom.find('#page-wrapper').html(data);
        alert(data);
      } else {
        alert('get data error !');
      }
    })
    return false;

  });

  dom.on('blur', '#one input', function() {
    var $that  = $(this);
    var fieldName = $that.attr('name');
    if(fieldName != 'crawlerUrl') {
      return false;
    }

    var $hasFeedback = $that.parents('.has-feedback');
    var fieldVal = $.trim($that.val());

    if(!fieldVal){
      $hasFeedback.addClass('has-error').find('.glyphicon').addClass('glyphicon-remove');
      dom.find('#crawler-url-btn').addClass('disable');
      dom.find('#crawler-plane-url-btn').addClass('disable');
    } else {
      $hasFeedback.removeClass('has-error').find('.glyphicon').removeClass('glyphicon-remove');
      dom.find('#crawler-url-btn').removeClass('disable');
      dom.find('#crawler-plane-url-btn').removeClass('disable');
    }

  });

  dom.on('click', '#crawler-url-btn', function() {
    var crawlerUrl = $.trim(dom.find('#crawler-url').val());
    if(!crawlerUrl || $(this).hasClass('disable')) {
      return false;
    }

    $globalLoading.fadeIn(90);

    $.post('/admin/crawlerOneNews', {crawlerUrl: crawlerUrl}, function(data) {
      $globalLoading.fadeOut(0);

      if(data) {
        //dom.find('#page-wrapper').html(data);
        alert(data);
      } else {
        alert('get data error !');
      }
    })
    return false;

  });
  
  dom.on('click', '#crawler-plane-btn', function() {
    $globalLoading.fadeIn(90);

    $.post('/admin/crawlerManyPlane', function(data) {
      $globalLoading.fadeOut(0);

      if(data) {
        //dom.find('#page-wrapper').html(data);
        alert(data);
      } else {
        alert('get data error !');
      }
    })
    return false;

  });

  dom.on('click', '#crawler-plane-url-btn', function() {
    var crawlerUrl = $.trim(dom.find('#crawler-url').val());
    if(!crawlerUrl || $(this).hasClass('disable')) {
      return false;
    }
    $globalLoading.fadeIn(90);

    $.post('/admin/crawlerOnePlane', {crawlerUrl: crawlerUrl}, function(data) {
      $globalLoading.fadeOut(0);

      if(data) {
        //dom.find('#page-wrapper').html(data);
        alert(data);
      } else {
        alert('get data error !');
      }
    })
    return false;

  });


  dom.on('click', '#search-btn', function() {
    $keyword = dom.find('[name=keyword]');
    var keyword = $.trim($keyword.val());
    if(!keyword) {
      return;
    }
    var searchUrl = $keyword.attr('search-url');
    var chooseLink = $keyword.attr('chooseLink');
    var parentId = $keyword.attr('parentId');

    var url = searchUrl + '?keyword=' + $.trim(keyword);
    if(chooseLink) {
      url += '&chooseLink=' + chooseLink;
    }
    if(parentId) {
      url += '&parentId=' + parentId;
    }
    $.post(url, function(data) {
      if(data) {
        dom.find('#page-wrapper').html(data);
      } else {
        alert('get data error !');
      }
    })
    return false;
  });

  dom.on('blur', '#article-form input', function() {
    var $that  = $(this);
    var fieldName = $that.attr('name');
    if(fieldName != 'artibodyTitle') {
      return false;
    }

    var $hasFeedback = $that.parents('.has-feedback');
    var fieldVal = $.trim($that.val());

    if(!fieldVal){
      $hasFeedback.addClass('has-error').find('.glyphicon').addClass('glyphicon-remove');
      dom.find('#article-add-submit').addClass('disable');
      dom.find('#article-edit-submit').addClass('disable');
    } else {
      $hasFeedback.removeClass('has-error').find('.glyphicon').removeClass('glyphicon-remove');
      dom.find('#article-add-submit').removeClass('disable');
      dom.find('#article-edit-submit').removeClass('disable');
    }

  });

  dom.on('click', '#article-add-submit', function() {
    if($(this).hasClass('disable')) {
      return false;
    }

    var $articleForm = dom.find('#article-form');

    var artibodyTitle = $.trim($articleForm.find('[name=artibodyTitle]').val());
    var mediaName = $articleForm.find('[name=mediaName]').val();
    var medialink = $articleForm.find('[name=medialink]').val();
    var photo = $articleForm.find('[name=photo]').val();
    var pubDate = $articleForm.find('[name=pubDate]').val();
    var keywords = [];
    $articleForm.find('[name=keywords]').each(function(){
      var value = $.trim($(this).val());
      if(value) {
        keywords.push(value);
      }
    });
    var artibody = $.trim(editor.html());

    if(!artibodyTitle || !artibody) {
      return false;
    }

    var postdata = {
      artibodyTitle: artibodyTitle,
      mediaName: mediaName,
      medialink: medialink,
      photo: photo,
      pubDate: pubDate,
      keywords: keywords,
      artibody: artibody
    };

    var url = '/admin/saveNewsArticle';
    if($.trim($(this).attr('data-isPlane'))){
      url = '/admin/savePlaneArticle'
    }
    
    $.post(url, postdata, function(data) {
      if(data) {
        $('#page-wrapper').html(data);
      } else {
        alert('get data error !');
      }
    });
    return false;
  });

  dom.on('click', '#article-edit-submit', function() {
    if($(this).hasClass('disable')) {
      return false;
    }

    var artId = $(this).attr('data-artId');
    var $articleForm = dom.find('#article-form');

    var artibodyTitle = $.trim($articleForm.find('[name=artibodyTitle]').val());
    var mediaName = $articleForm.find('[name=mediaName]').val();
    var medialink = $articleForm.find('[name=medialink]').val();
    var photo = $articleForm.find('[name=photo]').val();
    var pubDate = $articleForm.find('[name=pubDate]').val();
    var keywords = [];
    $articleForm.find('[name=keywords]').each(function(){
      var value = $.trim($(this).val());
      if(value) {
        keywords.push(value);
      }
    });
    var artibody = $.trim(editor.html());

    if(!artibodyTitle || !artibody) {
      return false;
    }

    var postdata = {
      artId: artId,
      artibodyTitle: artibodyTitle,
      mediaName: mediaName,
      medialink: medialink,
      photo: photo,
      pubDate: pubDate,
      keywords: keywords,
      artibody: artibody
    };

    var url = '/admin/updateNewsArticle';
    if($.trim($(this).attr('data-isPlane'))){
      url = '/admin/updatePlaneArticle'
    }
    
    $.post(url, postdata, function(data) {
      if(data) {
        $('#page-wrapper').html(data);
      } else {
        alert('get data error !');
      }
    });
    return false;
  });


  dom.on('click', '#add-param-btn', function() {

    $(this).siblings('table').find('tbody').append('<tr><td class="title-name"><input name="" type="text" class="form-control" value=""></td><td><input name="" type="text" class="form-control" value=""></td><td class="handle-td"><a class="btn btn-danger remove-tr">删除</a></td></tr>');

  });

  dom.on('click', '#add-other-btn', function() {

    $(this).siblings('table').find('tbody').append('<tr><td class="title-name"><input name="" type="text" class="form-control" value=""></td><td><input name="" type="text" class="form-control" value=""></td><td class="handle-td"><a class="btn btn-danger remove-tr">删除</a></td></tr>');

  });

  dom.on('click', '.form-table .remove-tr', function() {

    $(this).parents('tr').remove();

  });

  dom.on('blur', '#plane-form input', function() {
    var $that  = $(this);
    var fieldName = $that.attr('name');
    if(fieldName != 'planeName') {
      return false;
    }

    var $hasFeedback = $that.parents('.has-feedback');
    var fieldVal = $.trim($that.val());

    if(!fieldVal){
      $hasFeedback.addClass('has-error').find('.glyphicon').addClass('glyphicon-remove');
      dom.find('#plane-add-submit').addClass('disable');
      dom.find('#plane-edit-submit').addClass('disable');
    } else {
      $hasFeedback.removeClass('has-error').find('.glyphicon').removeClass('glyphicon-remove');
      dom.find('#plane-add-submit').removeClass('disable');
      dom.find('#plane-edit-submit').removeClass('disable');
    }

  });

   dom.on('click', '#plane-edit-submit', function() {
    var plaId = $(this).attr('data-plaId');
    var $planeForm = dom.find('#plane-form');

    var planeName = $.trim($planeForm.find('[name=planeName]').val());
    var photo = $.trim($planeForm.find('[name=photos]').val());
    var photos = [photo];
    var ellipsis = $.trim($planeForm.find('[name=ellipsis]').val());
    var manufacturer = $.trim($planeForm.find('[name=manufacturer]').val());
    var firstTime = $.trim($planeForm.find('[name=firstTime]').val());
    var detailKey = $planeForm.find('[name=detailKey]').val();

    var parameters = [];
    $planeForm.find('tr.parameters').each(function(){
      var name = $.trim($(this).find('[name=parametersName]').val());
      var value = $.trim($(this).find('[name=parametersValue]').val());
      if(name && value) {
        parameters.push({name:name, value:value});
      }
    });

    var others = [];
    $planeForm.find('tr.others').each(function(){
      var name = $.trim($(this).find('[name=othersName]').val());
      var description = $.trim($(this).find('[name=othersDescription]').val());
      if(name && description) {
        others.push({name:name, description:description});
      }
    });

    if(!planeName) {
      return false;
    }

    var postdata = {
      plaId: plaId,
      planeName: planeName,
      photos: photos,
      ellipsis: ellipsis,
      manufacturer: manufacturer,
      firstTime: firstTime,
      parameters: parameters,
      others: others,
      detailKey: detailKey
    };

    var newdata = JSON.stringify(postdata);

    var url = '/admin/updatePlane';
    
    $.post(url, {postdata: newdata}, function(data) {
      if(data) {
        $('#page-wrapper').html(data);
      } else {
        alert('get data error !');
      }
    });
    return false;
  });
  
  dom.on('click', '#plane-add-submit', function() {
    var $planeForm = dom.find('#plane-form');

    var planeName = $.trim($planeForm.find('[name=planeName]').val());
    var photo = $.trim($planeForm.find('[name=photos]').val());
    var photos = [photo];
    var ellipsis = $.trim($planeForm.find('[name=ellipsis]').val());
    var manufacturer = $.trim($planeForm.find('[name=manufacturer]').val());
    var firstTime = $.trim($planeForm.find('[name=firstTime]').val());
    var detailKey = $planeForm.find('[name=detailKey]').val();

    var parameters = [];
    $planeForm.find('tr.parameters').each(function(){
      var name = $.trim($(this).find('[name=parametersName]').val());
      var value = $.trim($(this).find('[name=parametersValue]').val());
      if(name && value) {
        parameters.push({name:name, value:value});
      }
    });

    var others = [];
    $planeForm.find('tr.others').each(function(){
      var name = $.trim($(this).find('[name=othersName]').val());
      var description = $.trim($(this).find('[name=othersDescription]').val());
      if(name && description) {
        others.push({name:name, description:description});
      }
    });

    if(!planeName) {
      return false;
    }

    var postdata = {
      planeName: planeName,
      photos: photos,
      ellipsis: ellipsis,
      manufacturer: manufacturer,
      firstTime: firstTime,
      parameters: parameters,
      others: others,
      detailKey: detailKey
    };

    var newdata = JSON.stringify(postdata);

    var url = '/admin/savePlane';
    
    $.post(url, {postdata: newdata}, function(data) {
      if(data) {
        $('#page-wrapper').html(data);
      } else {
        alert('get data error !');
      }
    });
    return false;
  });

  dom.on('blur', '#user-form input', function() {
    var $that  = $(this);
    var $hasFeedback = $that.parents('.has-feedback');
    var fieldName = $that.attr('name');
    var fieldVal = $.trim($that.val());

    if(!fieldVal || (fieldName=='email' && !/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(fieldVal))){
      $hasFeedback.addClass('has-error').find('.glyphicon').addClass('glyphicon-remove');
      dom.find('#user-add-submit').addClass('disable');
      dom.find('#user-edit-submit').addClass('disable');
    } else {
      $hasFeedback.removeClass('has-error').find('.glyphicon').removeClass('glyphicon-remove');
      dom.find('#user-add-submit').removeClass('disable');
      dom.find('#user-edit-submit').removeClass('disable');
    }

  });

  dom.on('click', '#user-add-submit', function() {
    if($(this).hasClass('disable')) {
      return false;
    }

    var $userForm = dom.find('#user-form');

    var userName = $.trim($userForm.find('[name=userName]').val());
    var email = $userForm.find('[name=email]').val();
    var phone = $userForm.find('[name=phone]').val();
    var photo = $userForm.find('[name=photo]').val();
    var typeName = $userForm.find('[name=typeName]').val();
    var password = $userForm.find('[name=password]').val();

    if(!userName || !password || !email) {
      return false;
    }

    var postdata = {
      userName: userName,
      email: email,
      phone: phone,
      typeName: typeName,
      password: $.md5(password)
    };

    var url = '/admin/userSave';
    
    $.post(url, postdata, function(data) {
      if(data) {
        if(data == 'isExit') {
          alert('邮箱已被使用！');
          return;
        }
        $('#page-wrapper').html(data);
      } else {
        alert('get data error !');
      }
    });
    return false;
  });

  dom.on('click', '#user-edit-submit', function() {
    if($(this).hasClass('disable')) {
      return false;
    }

    var userId = $(this).attr('data-userId');
    var $userForm = dom.find('#user-form');

    var userName = $.trim($userForm.find('[name=userName]').val());
    var email = $userForm.find('[name=email]').val();
    var phone = $userForm.find('[name=phone]').val();
    var photo = $userForm.find('[name=photo]').val();
    var typeName = $userForm.find('[name=typeName]').val();
    var password = $userForm.find('[name=password]').val();

    if(!userName || !email) {
      return false;
    }

    var postdata = {
      userId: userId,
      userName: userName,
      email: email,
      phone: phone,
      typeName: typeName
    };

    if(password) {
      postdata.password = $.md5(password)
    }

    var url = '/admin/updateUser';
    
    $.post(url, postdata, function(data) {
      if(data && data == 'login') {
        window.location.href = '/login';
      } else if(data) {
        $('#page-wrapper').html(data);
      } else {
        alert('get data error !');
      }
    });
   return false;
  });


  dom.on('click', '#chat-count-btn', function() {

    var num = $.trim(dom.find('#chat-count').val());
    var url = $(this).attr('data-url');
    $.post(url, {num: num}, function(data) {
      if(data) {
        dom.find('#page-wrapper').html(data);
      } else {
        alert('get data error !');
      }
    })
    return false;

  });

}
